package com.gxa.controller.community;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.communitydto.BuildingsDto;
import com.gxa.entity.community.Buildings;
import com.gxa.entity.community.Community;
import com.gxa.entity.discrepancy.Device;
import com.gxa.service.community.BuildingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;
import static com.gxa.common.utils.Result.failed;
import static com.gxa.common.utils.Result.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:38
 */
@Api(tags = "楼栋管理接口")
@RestController
@RequestMapping("/buildings")
public class BuildingsController {

    @Autowired
    private BuildingsService buildingsService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "楼栋列表接口")
    @PostMapping("/list")
    public Result<PageInfo<Buildings>> queryAll(@RequestBody BuildingsDto buildingsDto, Integer pageNum, Integer pageSize){
        Map<String, Object> map = buildingsService.queryAll(buildingsDto, pageNum, pageSize);
        PageInfo<Buildings> pageInfo = (PageInfo) map.get("pageInfo");

//        List<Buildings> buildingsList = buildingsService.query();
        return Result.success(pageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "新增楼栋接口")
    @PostMapping("/add")
    public Result add(@RequestBody Buildings buildings){
        try{
            buildingsService.add(buildings);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "删除楼栋接口")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try{
            buildingsService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return failed("删除失败");
        }
        return success();
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "根据id查询楼栋接口")
    @GetMapping("/queryById/{id}")
    public Result<BuildingsDto> queryById(@PathVariable("id") Integer id){
        BuildingsDto buildings;
        try{
            buildings = buildingsService.queryById(id);
        }catch (Exception e){
            e.printStackTrace();
            return failed("查询失败");
        }
        return success(buildings);
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "编辑楼栋接口")
    @PutMapping("/edit")
    public Result edit(@RequestBody Buildings buildings){
        try{
            int i = 0/1;
            buildingsService.update(buildings);
        }catch (Exception e){
            e.printStackTrace();
            return failed("更新失败");
        }
        return success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "异步请求判断楼栋编号是否重复的接口")
    @GetMapping("/judgeOne")
    public Result judgeOne(String buildNum){
        List<Buildings> buildingsList = buildingsService.queryByNum(buildNum);
        if (buildingsList.size() != 0) {
            return Result.failed("楼栋编码重复了，请重新输入");
        } else {
            return Result.success();
        }
    }
}
